<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        // https://source.unsplash.com/500x400?animal
        //  <Image url="http://awef.com/1231.jpg"  />
        class Image extends React.Component{
            render(){
                console.log('4-render渲染');
                let {url} = this.props;
                return <img src={url} alt="" />;
            }

            //1. 将要接受到父级组件传入的属性
            // will 将要  receive 接收  props 属性
            componentWillReceiveProps(){
                console.log('1-将要接受父级传入的属性 - componentWillReceiveProps')
            }

            //2. shouldComponentUpdate 组件应该要更新 
            shouldComponentUpdate(){
                console.log('2-组件应该要更新 - shouldComponentUpdate');
                return true;
            }

            //3. componentWillUpdate 组件将要更新
            componentWillUpdate(){
                console.log('3-组件将要更新 - componentWillUpdate')
            }

            //5. componentDidUpdate 组件完成了更新
            componentDidUpdate(){
                console.log('5-组件完成了更新 - componentDidUpdate');
            }

        }

        class App extends React.Component{

            state = {
                keyword: '' // animal   car   game
            }

            change = (e) => {
                // console.log(e.target.dataset);
                //更新 keyword 的值
                this.setState({
                    keyword: e.target.dataset.k
                })
            }

            render(){
                return <div>
                        <button onClick={this.change} data-k="animal" data-a="123" data-xyz="456">动物</button>
                        <button onClick={this.change} data-k="car">汽车</button>
                        <button onClick={this.change} data-k="game">游戏</button>
                        <hr />
                        <Image url={"https://source.unsplash.com/500x400?"+this.state.keyword} />
                    </div>
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>